package display.scrollableWindow {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import org.ilovecode.mvc.View;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class ScrollableWindow extends View {
		
		//{ region Stage elements
		
		public var scrollbar:Scrollbar;
		public var content:MovieClip;
		public var scrollMask:MovieClip;
		
		//} endregion
		
		private const CONTENT_OFFSET:int = 10;
		
		public function ScrollableWindow() {
			//(this.stage != null) ? construct() : this.addEventListener(Event.ADDED_TO_STAGE, construct);
			super();
		}
		
		override protected function construct():void
		{
			scrollbar = Scrollbar(this.getChildByName("scrollbar"));
			content = MovieClip(this.getChildByName("content"));
			scrollMask = MovieClip(this.getChildByName("scrollMask"));
		}
		
		/*private function construct(event:Event=null):void
		{
			if (this.hasEventListener(Event.ADDED_TO_STAGE)) this.removeEventListener(Event.ADDED_TO_STAGE, construct);;
			
			scrollbar = Scrollbar(this.getChildByName("scrollbar"));
			content = MovieClip(this.getChildByName("content"));
			scrollMask = MovieClip(this.getChildByName("scrollMask"));
		}*/
		
		public function configUI(width:Number, height:Number):void {
			scrollMask.width = width;
			scrollMask.height = height;
			
			scrollbar.x = width - scrollbar.width;
			scrollbar.scrollArea.height = height;
		}
		
		
		public function setContent(content:Sprite):void {
			this.content.addChild(content);
			init();
		}
		
		public function clear():void {
			while (this.content.numChildren > 0) {
				this.content.removeChildAt(0);
			}
		}
		
		private function init():void {
			initListeners();
			scrollbar.visible = (content.height > scrollbar.height);
		}
		
		private function initListeners():void {
			scrollbar.addEventListener(Event.SCROLL, scrollbar_Scroll);
			scrollbar.addEventListener(Event.COMPLETE, scrollbar_Complete);
			content.addEventListener(Event.RESIZE, content_Resize);
			this.addEventListener(MouseEvent.MOUSE_WHEEL, this_MouseWheel);
		}
		
		//{ region Handlers
		
		private function content_Resize(event:Event):void {
			var scrollingArea:Number = content.height + CONTENT_OFFSET - scrollMask.height;
			scrollbar.setScrollPositionRatio(- content.y / scrollingArea);
		}
		
		private function scrollbar_Scroll(event:Event):void {
			scrollbar.addEventListener(Event.ENTER_FRAME, scrollContent);
		}
		
		private function scrollbar_Complete(event:Event):void {
			scrollbar.removeEventListener(Event.ENTER_FRAME, scrollContent);
		}
		
		private function this_MouseWheel(event:MouseEvent):void {
			scrollbar.moveHandle(event.delta);
			scroll();
		}
		
		//} endregion
		
		private function scrollContent(event:Event):void {
			scroll();
		}
		
		private function scroll():void {
			var scrollingArea:Number = content.height + CONTENT_OFFSET - scrollMask.height;
			//var scrollingArea:Number = content.height;
			var contentPosition:Number = - scrollbar.getScrollPositionRatio() * scrollingArea;
			TweenLite.to(content, 0.2, { y:contentPosition, ease:Linear.easeOut } );
		}
		
	}

}